﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework;
using OLTS.DAL;
using OLTS.MODEL;

namespace OLTS.DATA_SERVICE
{
    public class FAMILY_Service
    {
        private OTS_FAMILY  _objApp = new OTS_FAMILY();

        public FAMILY_Service(OTS_FAMILY objApp)
        {
            _objApp = objApp;
        }

        public FAMILY_Service()
        {

        }

        public List<OTS_FAMILY> GetAll()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_FAMILY.ToList();
                return list;
            }
        }

        public List<OTS_FAMILY> GetByCurentLogin(string currentuser)
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_FAMILY.Where(x=> x.EMPLOYEE_ID == currentuser).ToList();
                return list;
            }
        }
        public void GetAllinclude()
        {

        }

        public void Add(List<OTS_FAMILY> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Create)
                {
                    var model = new OTS_FAMILY();
                    model.EMPLOYEE_ID = obj.EMPLOYEE_ID;
                    model.NAMEOFFATHER = obj.NAMEOFFATHER;
                    model.F_OCCUPATION = obj.F_OCCUPATION;
                    model.NAMEOFMOTHER = obj.NAMEOFMOTHER;
                    model.M_OCCUPATION = obj.M_OCCUPATION;
                    model.NUMOFBRO_SIS = obj.NUMOFBRO_SIS;
                    using (var ctx = new Entities())
                    {
                        ctx.OTS_FAMILY.AddObject(model);
                        ctx.SaveChanges();
                    }
                }
            }
        }

        public void Edit(List<OTS_FAMILY> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Update)
                {
                    using (var ctx = new Entities())
                    {
                        var objEdit = ctx.OTS_FAMILY.Where(x => x.ID == obj.ID).FirstOrDefault();
                        if (objEdit != null)
                        {
                            objEdit.EMPLOYEE_ID = obj.EMPLOYEE_ID;
                            objEdit.NAMEOFFATHER = obj.NAMEOFFATHER;
                            objEdit.F_OCCUPATION = obj.F_OCCUPATION;
                            objEdit.NAMEOFMOTHER = obj.NAMEOFMOTHER;
                            objEdit.M_OCCUPATION = obj.M_OCCUPATION;
                            objEdit.NUMOFBRO_SIS = obj.NUMOFBRO_SIS;
                            ctx.SaveChanges();
                        }
                    }
                }
            }
        }

        public void Delete()
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_FAMILY.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                ctx.OTS_FAMILY.DeleteObject(obj);
                ctx.SaveChanges();
            }

        }

    }
}
